home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Util / MemoryUtil.c < prev    next >
Text File  |  1996-04-12  |  3KB  |  224 lines

  1. /*****
  2.  *
  3.  *    MemoryUtil.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #include "compiler_stuff.h"
  18. #include "globals.h"
  19.  
  20. #include "MemoryUtil.h"
  21.  
  22.  
  23. /***  LOCAL PROTOTYPES  ***/
  24.  
  25. pascal    long    memoryGrowZone    ( Size );
  26.  
  27.  
  28. /***  MEMORY ALLOCATION  ***/
  29.  
  30. /*    I use these 'wrappers' to toolbox memory calls to provide a way
  31.     to consistantly handle error conditions. */
  32. p_export
  33. Handle
  34. MemoryNewHandle ( long theSize, OSErr *outErr )
  35. {
  36.     Handle    theHandle;
  37.     OSErr    theErr;
  38.     
  39.     theHandle    = NewHandle    ( theSize );
  40.     theErr        = MemError    ();
  41.     
  42.     if ( outErr != NULL )
  43.     {
  44.         *outErr = theErr;
  45.     }
  46.     
  47.     if ( theErr != noErr )
  48.     {
  49.         return NULL;
  50.     }
  51.     else
  52.     {
  53.         return theHandle;
  54.     }
  55. }
  56.  
  57. p_export
  58. Handle
  59. MemoryNewHandleClear ( long theSize, OSErr *outErr )
  60. {
  61.     Handle    theHandle;
  62.     OSErr    theErr;
  63.     
  64.     theHandle    = NewHandleClear ( theSize );
  65.     theErr        = MemError         ();
  66.     
  67.     if ( outErr != NULL )
  68.     {
  69.         *outErr = theErr;
  70.     }
  71.     
  72.     if ( theErr != noErr )
  73.     {
  74.         return NULL;
  75.     }
  76.     else
  77.     {
  78.         return theHandle;
  79.     }
  80. }
  81.  
  82. p_export
  83. Ptr
  84. MemoryNewPtr ( long theSize, OSErr *outErr )
  85. {
  86.     Ptr        thePtr;
  87.     OSErr    theErr;
  88.     
  89.     thePtr    = NewPtr    ( theSize );
  90.     theErr    = MemError    ();
  91.     
  92.     if ( outErr != NULL )
  93.     {
  94.         *outErr = theErr;
  95.     }
  96.     
  97.     if ( theErr != noErr )
  98.     {
  99.         return NULL;
  100.     }
  101.     else
  102.     {
  103.         return thePtr;
  104.     }
  105. }
  106.  
  107. p_export
  108. Ptr
  109. MemoryNewPtrClear ( long theSize, OSErr *outErr )
  110. {
  111.     Ptr        thePtr;
  112.     OSErr    theErr;
  113.     
  114.     thePtr    = NewPtrClear    ( theSize );
  115.     theErr    = MemError        ();
  116.     
  117.     if ( outErr != NULL )
  118.     {
  119.         *outErr = theErr;
  120.     }
  121.     
  122.     if ( theErr != noErr )
  123.     {
  124.         return NULL;
  125.     }
  126.     else
  127.     {
  128.         return thePtr;
  129.     }
  130. }
  131.  
  132.  
  133. /***  EMERGENCY MEMORY  ***/
  134. #pragma mark -
  135.  
  136. /* IM-Memory: 1-46 */
  137. Boolean
  138. InitializeEmergencyMemory ( OSErr *outErr )
  139. {
  140.     Boolean     success;
  141.     OSErr        theErr;
  142.     
  143.     success = true;
  144.     
  145.     gEmergencyMemory = NewHandle ( kMemCushionSize );
  146.     theErr             = MemError  ();
  147.     
  148.     if ( outErr != NULL )
  149.     {
  150.         *outErr = theErr;
  151.     }
  152.     
  153.     if ( theErr != noErr )
  154.     {
  155.         gEmergencyMemory = NULL;
  156.         success             = false;
  157.     }
  158.     else
  159.     {
  160.         SetGrowZone ( NewGrowZoneProc(memoryGrowZone) );
  161.     }
  162.     
  163.     return success;
  164. } /* InitializeEmergencyMemory */
  165.  
  166.  
  167. /* IM-Memory: 1-48 */
  168. pascal long
  169. memoryGrowZone ( Size cbNeeded )
  170. {
  171.     OSErr    theErr;
  172.     long    theA5;
  173.     long    theSize;
  174.     
  175.     theA5 = SetCurrentA5 ();
  176.     
  177.     if ( *gEmergencyMemory && ( gEmergencyMemory != GZSaveHnd() ) )
  178.     {
  179.         EmptyHandle ( gEmergencyMemory );
  180.         
  181.         theErr = MemError ();
  182.         
  183.         if ( theErr != noErr )
  184.         {
  185.             theSize = nil;
  186.         }
  187.         else
  188.         {
  189.             theSize = kMemCushionSize; /* kEmergencyMemorySize in IM */
  190.         }
  191.     }
  192.     else
  193.     {
  194.         theSize = nil;
  195.     }
  196.     
  197.     theA5 = SetA5 ( theA5 );
  198.     
  199.     return theSize;
  200. } /* memoryGrowZone */
  201.  
  202.  
  203. /* To check that the memory reserve is intact.
  204.     Returns true if there is emergency memory on reserve.
  205.     IM-Memory: 1-47 */
  206. Boolean
  207. IsEmergencyMemAvail ( void )
  208. {
  209.     return ( (gEmergencyMemory != NULL) && (*gEmergencyMemory != NULL) );
  210. } /* IsEmergencyMemAvail */
  211.  
  212.  
  213. /* IM-Memory: 1-48 */
  214. #if !(kCompileWithQuitOnLowMemory)
  215. OSErr
  216. RecoverEmergencyMemory ( void )
  217. {
  218.     ReallocateHandle ( gEmergencyMemory, kMemCushionSize );
  219.     
  220.     return MemError ();
  221. } /* RecoverEmergencyMemory */
  222. #endif
  223.  
  224. /***  EOF  ***/